<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/29
 * Time: 21:36
 */

include_once 'redis.php';//引入redis类

$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$age = $_POST['age'];

$prevInfo = 'prev_' . get_unique_num();
$redis->hMset($prevInfo, $_POST);// 保存上一次填写信息
$redis->expire($prevInfo, $redisSessionTime);// 上一次填写信息设置时效

$url = "index.php?key={$prevInfo}";//注册失败跳转url
if (empty($username)) {
    notice('账号不能为空');
}
if (empty($password)) {
    notice('密码不能为空');
}
if (empty($name)) {
    notice('姓名不能为空');
}
if (empty($phone)) {
    notice('电话不能为空');
}
if (empty($age)) {
    notice('年龄不能为空');
}

//注册

//自增uid
$ret = $redis->incr('uid');
if (!$ret) {
    notice('自增uid失败');
}

//获取自增uid
$uid = $redis->get('uid');
if (!$uid) {
    notice('获取自增uid失败');
}

//添加用户数组
$ret = $redis->rPush('userIds', $uid);
if (!$ret) {
    notice('添加用户数组失败');
}

//添加账号信息
$ret = $redis->set("username:{$username}", $uid);
if (!$ret) {
    notice('添加账号信息失败');
}

//添加用户信息
$ret = $redis->hMset("user:{$uid}", [
    'uid' => $uid,
    'username' => $username,
    'password' => md5($password),
    'name' => $name,
    'phone' => $phone,
    'age' => $age
]);
if (!$ret) {
    notice('添加用户信息失败');
}

notice('注册成功', 'login.php');// 注册成功跳登录页面
?>
